﻿///<reference path="../../vswd-ext_2.0.2.js" />
Ext.BLANK_IMAGE_URL = '../../Images/s.gif';

//性别 
var funcTypeDs = new Ext.data.Store({
    url: '../../Trans/Base/Arg.aspx',
    baseParams: {
        action: 'arg',
        arg_type: EF.UI.Const.ArgTypeMark.SYSTEM_FUNCTION
    },
    reader: new Ext.data.JsonReader({
        root: 'rows'
    }, [
        'ArgId',
        'ArgName'
    ])
});

Ext.onReady(function() {
    Ext.QuickTips.init();

    var urlArgObj = Ext.urlDecode(window.location.search.substring(1, window.location.search.length));
    var tabHeight = urlArgObj['h'] - 1; //the height of tab panel
    var tabWidth = urlArgObj['w'] - 1; //the width of tab panel
    var pageId = urlArgObj['page_id']; //the function id of the page

    Ext.Ajax.request({
        url: '../../Trans/Base/Lang.aspx',
        params: {
            action: 'lang',
            marks: "'index','btn_close','msg_confirm_to_del','btn_cancel','msg_function_has_children','msg_select_single_to_del','title_view','msg_select_one_to_eidt','title_edit','title_tip','btn_sure','msg_select_one_or_none','btn_save','lbl_func_type','lbl_func_name_cn','lbl_func_name_en','lbl_func_name_ja','lbl_descr','lbl_func_url','lbl_sort_index','max_length_text','blank_text','title_add','msg_invalid_sort_index'"
        },
        success: function(response, opts) {
            var langObj = Ext.decode(response.responseText);
            langObj = langObj['rows'][0];

            //加载数据
            funcTypeDs.load();

            //表单 begin
            var action = null; //操作

            //排序验证
            Ext.form.VTypes['SortIndexVal'] = /^[1-9]\d*|0$/;
            Ext.form.VTypes['SortIndexText'] = langObj['msg_invalid_sort_index'];
            Ext.form.VTypes['SortIndex'] = function(v) {
                return Ext.form.VTypes['SortIndexVal'].test(v);
            };

            var funcFormPanel = new Ext.FormPanel({
                border: false,
                width: 375,
                labelWidth: 150,
                labelAlign: 'right',
                buttonAlign: 'center',
                bodyStyle: 'padding:5px 5px 0;background:transparent;',
                items: [{
                    xtype: 'combo',
                    store: funcTypeDs,
                    name: 'FuncType',
                    displayField: 'ArgName',
                    valueField: 'ArgId',
                    forceSelection: true,
                    width: 150,
                    mode: 'local',
                    fieldLabel: langObj['lbl_func_type'],
                    emptyText: langObj['combo_empty_text'],
                    triggerAction: 'all',
                    allowBlank: false,
                    blankText: langObj['blank_text']
                }, {
                    xtype: 'hidden',
                    name: 'FuncId'
                }, {
                    xtype: 'textfield',
                    width: 150,
                    name: 'FuncNameCn',
                    fieldLabel: langObj['lbl_func_name_cn'],
                    maxLength: 100,
                    maxLengthText: langObj['max_length_text'],
                    allowBlank: false,
                    blankText: langObj['blank_text']
                }, {
                    xtype: 'textfield',
                    width: 150,
                    name: 'FuncNameEn',
                    fieldLabel: langObj['lbl_func_name_en'],
                    maxLength: 100,
                    maxLengthText: langObj['max_length_text'],
                    allowBlank: false,
                    blankText: langObj['blank_text']
                }, {
                    xtype: 'textfield',
                    width: 150,
                    name: 'FuncNameJa',
                    fieldLabel: langObj['lbl_func_name_ja'],
                    maxLength: 100,
                    maxLengthText: langObj['max_length_text']
                }, {
                    xtype: 'textfield',
                    width: 150,
                    name: 'URL',
                    fieldLabel: langObj['lbl_func_url'],
                    maxLength: 100,
                    maxLengthText: langObj['max_length_text']
                }, {
                    xtype: 'textfield',
                    width: 150,
                    name: 'Descr',
                    fieldLabel: langObj['lbl_descr'],
                    maxLength: 200,
                    maxLengthText: langObj['max_length_text']
                }, {
                    xtype: 'textfield',
                    width: 150,
                    name: 'SortIndex',
                    fieldLabel: langObj['lbl_sort_index'],
                    maxLength: 10,
                    maxLengthText: langObj['max_length_text'],
                    allowBlank: false,
                    blankText: langObj['blank_text'],
                    vtype: 'SortIndex'
                }],
                buttons: [{
                    id: 'btn_save',
                    text: langObj['btn_save'],
                    width: 60,
                    handler: function() {
                        var funcForm = funcFormPanel.getForm();
                        if (funcForm.isValid()) {
                            win.hide(null, function() {
                                var model = '{';
                                model += "'FuncNameCn':'" + funcForm.findField('FuncNameCn').getValue() + "',";
                                model += "'FuncNameEn':'" + funcForm.findField('FuncNameEn').getValue() + "',";
								model += "'FuncType':'" + funcForm.findField('FuncType').getValue() + "',";
								
								if (action == 'add')
								{
									var selNode = tree.getChecked();
									if (selNode == 0) {
										model += "'FatherId':'0',";
									} else {
										model += "'FatherId':'" + selNode[0].id + "',";
									}
								}
								
                                if (funcForm.findField('FuncNameJa').getValue() != '') {
                                    model += "'FuncNameJa':'" + funcForm.findField('FuncNameJa').getValue() + "',"; 
                                }
                                
                                if (funcForm.findField('URL').getValue() != '') {
                                    model += "'URL':'" + funcForm.findField('URL').getValue() + "',";
                                }

                                if (funcForm.findField('Descr').getValue() != '') {
                                    model += "'Descr':'" + funcForm.findField('Descr').getValue() + "',";
                                }

                                if (funcForm.findField('FuncId').getValue() != '') {
                                    model += "'FuncId':'" + funcForm.findField('FuncId').getValue() + "',";
                                }

                                model += "'SortIndex':'" + funcForm.findField('SortIndex').getValue() + "'}";

								Ext.Ajax.request({
									url: '../../Trans/Base/Function.aspx',
									params: {
										action: action,
										model: model
									},
									success: function(response, opts) {
										var obj = Ext.decode(response.responseText);
										
										if (obj['success'] == true) {
											Ext.Msg.show({
												title: langObj['title_tip'],
												msg: obj['msg'],
												buttons: {yes: langObj['btn_sure']},
												icon: Ext.MessageBox.INFO
											});
											
											treeLoader.load(rootNode, null, this);
										} else {
											Ext.Msg.show({
												title: langObj['title_tip'],
												msg: obj['msg'],
												buttons: {yes: langObj['btn_sure']},
												icon: Ext.MessageBox.ERROR
											});
										}
									},
									failure: function(response, opts) {
										var obj = Ext.decode(response.responseText);
										Ext.Msg.show({
											title: langObj['title_tip'],
											msg: obj['msg'],
											buttons: {yes: langObj['btn_sure']},
											icon: Ext.MessageBox.ERROR
										});
									}
								});
                            },this);
                        }
                    }
                }, {
                    text: langObj['btn_close'],
                    width: 60,
                    handler: function() {
                        win.hide(null, null, this);
                    }
                }]
            });

            //显示窗体
            var win = new Ext.Window({
                modal: true,
                resizable: false,
                closable: false,
                width: 375,
                height: 270,
                items: [funcFormPanel]
            });

            //表单 end

            //给表单设置初始值
            function funcFormSetDefault() {
                var funcForm = funcFormPanel.getForm();

                funcForm.items.each(function(item, index, lenght) {
                    item.enable();

                    if (item.getXType() == 'combo') {
                        item.setRawValue(langObj['combo_empty_text']);
						item.disable();
                    } else {
                        item.setRawValue();
                    }
                }, this);

                funcFormPanel.getFooterToolbar().findById('btn_save').enable(); //激活保存按钮
            }

            var treeLoader = new Ext.tree.TreeLoader({
                dataUrl: '../../Trans/Base/Function.aspx?action=func_tree'
            });

            var rootNode = new Ext.tree.AsyncTreeNode({
                text: langObj['index'],
                leaf: false,
                expanded: true
            });

            //刷新
            function funcRefresh() {
                treeLoader.load(rootNode, null, this);
            }

            //新建
            function funcAdd() {
                funcFormSetDefault();
                action = EF.UI.Const.FuncPoints.ADD;

				var selNode = tree.getChecked();
				if (selNode.length > 1)
				{
					Ext.Msg.show({
						title: langObj['title_tip'],
                        msg: langObj['msg_select_one_or_none'],
                        buttons: {yes: langObj['btn_sure']},
                        icon: Ext.MessageBox.INFO
                    });

					return;
				} 
				else if (selNode.length == 0)
				{
					funcFormPanel.getForm().findField('FuncType').setValue(EF.UI.Const.FuncMark.MODULE_ID);
				}
				else
				{
					if (selNode[0].attributes.ArgMark == EF.UI.Const.FuncMark.MODULE) //选择的为模块则新建页面
					{
						funcFormPanel.getForm().findField('FuncType').setValue(EF.UI.Const.FuncMark.PAGE_ID);
					}
					else if (selNode[0].attributes.ArgMark == EF.UI.Const.FuncMark.PAGE) //选择页面则新建功能点
					{
						funcFormPanel.getForm().findField('FuncType').setValue(EF.UI.Const.FuncMark.FUNCTION_POINT_ID);
					}
					else
					{
						return;
					}
				}

                win.setTitle(langObj['title_add']);
                win.show(null, null, this);
            }

            //编辑
            function funcEdit() {
				var selNode = tree.getChecked();
				if (selNode.length > 1 || selNode.length == 0) {
					Ext.Msg.show({
						title: langObj['title_tip'],
						msg: langObj['msg_select_one_to_eidt'],
						buttons: {yes: langObj['btn_sure']},
						icon: Ext.MessageBox.INFO
					});
				} else {
					Ext.Ajax.request({
						url: '../../Trans/Base/Function.aspx',
						params: {
							action: 'model',
							sel_func_id: selNode[0].id
						},
						success: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							if (obj['success'] == true) {
								var funcModel = obj['rows'];
								
								funcFormSetDefault();
								action = 'edit';
								
								var funcForm = funcFormPanel.getForm();
								funcForm.findField('FuncType').setValue(funcModel['FuncType']);
								funcForm.findField('FuncType').disable();
								funcForm.findField('FuncId').setValue(funcModel['FuncId']);
								funcForm.findField('FuncNameCn').setValue(funcModel['FuncNameCn']);
								funcForm.findField('FuncNameEn').setValue(funcModel['FuncNameEn']);
								funcForm.findField('FuncNameJa').setValue(funcModel['FuncNameJa']);
								funcForm.findField('URL').setValue(funcModel['URL']);
								funcForm.findField('Descr').setValue(funcModel['Descr']);
								funcForm.findField('SortIndex').setValue(funcModel['SortIndex']);
								
								win.setTitle(langObj['title_edit']);
								win.show(null,null,this);
							} else {
								Ext.Msg.show({
									title: langObj['title_tip'],
									msg: obj['msg'],
									buttons: {yes: langObj['btn_sure']},
									icon: Ext.MessageBox.ERROR
								});
							}
						},
						failure: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							Ext.Msg.show({
								title: langObj['title_tip'],
								msg: obj['msg'],
								buttons: {yes: langObj['btn_sure']},
								icon: Ext.MessageBox.ERROR
							});
						}
					});
				}
            }

            //查看
            function funcView() {
				var selNode = tree.getChecked();
				if (selNode.length > 1 || selNode.length == 0) {
					Ext.Msg.show({
						title: langObj['title_tip'],
						msg: langObj['msg_select_one_to_eidt'],
						buttons: {yes: langObj['btn_sure']},
						icon: Ext.MessageBox.INFO
					});
				} else {
					Ext.Ajax.request({
						url: '../../Trans/Base/Function.aspx',
						params: {
							action: 'model',
							sel_func_id: selNode[0].id
						},
						success: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							if (obj['success'] == true) {
								var funcModel = obj['rows'];
								
								funcFormSetDefault();
								
								var funcForm = funcFormPanel.getForm();
								funcForm.items.each(function(item, index, lenght) {
									item.disable();
								},this);
								
								var funcForm = funcFormPanel.getForm();
								funcForm.findField('FuncType').setValue(funcModel['FuncType']);
								funcForm.findField('FuncType').disable();
								funcForm.findField('FuncId').setValue(funcModel['FuncId']);
								funcForm.findField('FuncNameCn').setValue(funcModel['FuncNameCn']);
								funcForm.findField('FuncNameEn').setValue(funcModel['FuncNameEn']);
								funcForm.findField('FuncNameJa').setValue(funcModel['FuncNameJa']);
								funcForm.findField('URL').setValue(funcModel['URL']);
								funcForm.findField('Descr').setValue(funcModel['Descr']);
								funcForm.findField('SortIndex').setValue(funcModel['SortIndex']);
								
								funcFormPanel.getFooterToolbar().findById('btn_save').disable(); //禁止保存按钮
								
								win.setTitle(langObj['title_view']);
								win.show(null,null,this);
							} else {
								Ext.Msg.show({
									title: langObj['title_tip'],
									msg: obj['msg'],
									buttons: {yes: langObj['btn_sure']},
									icon: Ext.MessageBox.ERROR
								});
							}
						},
						failure: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							Ext.Msg.show({
								title: langObj['title_tip'],
								msg: obj['msg'],
								buttons: {yes: langObj['btn_sure']},
								icon: Ext.MessageBox.ERROR
							});
						}
					});
				}
            }

            //删除
            function funcPhysicalDelete() {
				var selNode = tree.getChecked();
				if (selNode.length > 1 || selNode.length == 0) {
					Ext.Msg.show({
						title: langObj['title_tip'],
						msg: langObj['msg_select_single_to_del'],
						buttons: {yes: langObj['btn_sure']},
						icon: Ext.MessageBox.INFO
					});
				} else {
					Ext.Msg.show({
                        title: langObj['title_tip'],
                        msg: langObj['msg_confirm_to_del'],
                        buttons: {yes: langObj['btn_sure'], no: langObj ['btn_cancel']},
                        icon: Ext.MessageBox.QUESTION,
                        fn: function(button, text) {
                            if (button == 'yes') {
                                var selFuncId = selNode[0].id;
                                
                                Ext.Ajax.request({
                                    url: '../../Trans/Base/Function.aspx',
                                    params: {
                                        action: 'del',
                                        sel_func_id: selFuncId
                                    },
                                    success: function(response, opts) {
                                        var obj = Ext.decode(response.responseText);
                                        if (obj['success'] == true) {
                                            Ext.Msg.show({
                                                title: langObj['title_tip'],
                                                msg: obj['msg'],
                                                buttons: {yes: langObj['btn_sure']},
                                                icon: Ext.MessageBox.INFO
                                            });
                                            
											if (obj['result'] == 1)
											{
												treeLoader.load(rootNode, null, this);
											}
                                        } else {
                                            Ext.Msg.show({
                                                title: langObj['title_tip'],
                                                msg: obj['msg'],
                                                buttons: {yes: langObj['btn_sure']},
                                                icon: Ext.MessageBox.ERROR
                                            });
                                        }
                                    },
                                    failure: function(response, opts) {
                                        var obj = Ext.decode(response.responseText);
                                        Ext.Msg.show({
                                            title: langObj['title_tip'],
                                            msg: obj['msg'],
                                            buttons: {yes: langObj['btn_sure']},
                                            icon: Ext.MessageBox.ERROR
                                        });
                                    }
                                });
                            }
                        }
                    });
                } 
            }

            var tree = new Ext.tree.TreePanel({
                renderTo: document.body,
                height: tabHeight,
                loader: treeLoader,
                root: rootNode,
                useArrows: true,
                autoScroll: true,
                animate: true,
                containerScroll: true,
                rootVisible: false
            });

            //生成功能点
            Ext.Ajax.request({
                url: '../../Trans/Base/Function.aspx',
                params: {
                    action: 'func_points',
                    page_id: pageId
                },
                success: function(response, opts) {
                    var obj = Ext.decode(response.responseText);

                    if (obj['success'] == true) {
                        var funcPointsObj = obj['rows'];
                        var tb = new Ext.Toolbar();
                        for (var propName in funcPointsObj) {
                            switch (propName) {
                                case EF.UI.Const.FuncPoints.REFRESH:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: funcRefresh
                                    });
                                    break;
                                case EF.UI.Const.FuncPoints.ADD:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: funcAdd
                                    });
                                    break;
                                case EF.UI.Const.FuncPoints.EDIT:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: funcEdit
                                    });
                                    break;
                                case EF.UI.Const.FuncPoints.VIEW:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: funcView
                                    });
                                    break;
                                case EF.UI.Const.FuncPoints.PHYSICALDEL:
                                    tb.add({
                                        text: funcPointsObj[propName],
                                        width: 60,
                                        handler: funcPhysicalDelete
                                    });
                                    break;
                            }
                        }

                        tree.add(tb);
                        tree.doLayout();
                    }
                },
                failure: function(response, opts) {

                }
            });



        },
        failure: function() {

        }
    });
});